package com.kehutong.user.dao;

import org.coraframework.orm.annotation.Dao;
import org.coraframework.orm.annotation.Mapper;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.Condition;
import org.coraframework.orm.jdbc.execute.QueryCondition;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Dao
public interface ResidentUserDao {

    @Mapper(
            genericType = HashMap.class,
            countSQL = "select COUNT(1) FROM  " +
                    " (SELECT  " +
                    "  DISTINCT  " +
                    " a.id,  " +
                    " a.realname,  " +
                    " a.mobile,  " +
                    " b.ownerNo," +
                    " b.communityNo,  " +
                    " b.communityName,  " +
                    " a.createTime  " +
                    " FROM  " +
                    " t_resident a,  " +
                    " t_resident_house b  " +
                    " WHERE  " +
                    " 1 = 1  " +
                    " AND a.id = b.residentNo  " +
                    " And b.houseType='1'" +
                    " AND b.bindType IN('0','1')  " +
                    " AND b.deleted = FALSE  " +
                    " AND ${}) t",
            sql = "SELECT" +
                    "  DISTINCT" +
                    "  a.id," +
                    "  a.realname," +
                    "  a.mobile," +
                    "  b.ownerNo," +
                    "  b.communityNo," +
                    "  b.communityName," +
                    "  a.createTime" +
                    " FROM" +
                    " t_resident a," +
                    " t_resident_house b" +
                    " WHERE" +
                    " 1 = 1" +
                    " AND a.id = b.residentNo" +
                    " And b.houseType='1'" +
                    " AND b.bindType IN('0','1')" +
                    " AND b.deleted = FALSE" +
                    " AND ${}"
    )
    Page<Map<String, Object>> loadPage(Condition... condition);


    @Mapper(
            genericType = String.class,
            sql = "SELECT DISTINCT id FROM t_resident t WHERE ${}"
    )
    List<String> getFriendResidentNos(Condition... condition);



    @Mapper(
            genericType = String.class,
            sql = "SELECT DISTINCT t2.houseNo FROM `t_community_building` t1 " +
                    "LEFT JOIN `t_resident_house` t2 ON t1.id = t2.buildNo " +
                    "WHERE ${}"
    )
    List<String> getStewardHouseNos(QueryCondition query);

    @Mapper(
            genericType = String.class,
            sql = "select id from t_community WHERE ${}"
    )
    List<String> getStewardCommunity(QueryCondition query);


    @Mapper(
            genericType = HashMap.class,
            sql = "SELECT t1.id userNo, t1.realname chargeName, t1.avatar avatar, t2.id externalId " +
                    "FROM t_resident t1 " +
                    "LEFT JOIN t_wwout_resident t2 ON t1.id = t2.khtid " +
                    "WHERE ${}"
    )
    List<Map<String, String>> batchResidentExternalInfo(QueryCondition query);




}
